<script>
    $(function () {
        $('#page_table_address_${pid}').datagrid({
            fit: true,
            width: '100%',
            height: '100%',
            striped: true,
            pagination: true,
            url: "${contextPath}/security/basic/address_book/page.htm?passportId=${passportId}",
            fitColumns: true,
            pageSize: 10,
            pageList: [10, 50, 100],
            idField: 'id',
            singleSelect: true,
            selectOnCheck: false,
            checkOnSelect: false,
            autoRowHeight: false,
            rowStyler: gridRowStyler,
            columns: [
                [
                    {
                        title: '联系人',
                        align: 'center',
                        field: 'contact',
                        width: 40
                    },
                    {
                        title: '电话',
                        align: 'center',
                        field: 'tel',
                        width: 60
                    },
                    {
                        title: '地址',
                        align: 'center',
                        field: 'address',
                        width: 100,
                        formatter: function (val, row) {
                            return (row.provinceName || '') + (row.cityName || '') + (row.districtName || '') + (row.street || '');
                        }
                    },
                    {
                        title: '默认地址',
                        align: 'center',
                        field: 'defaultAddress',
                        width: 40,
                        formatter: function (val, row) {
                            return val == 1 ? '是' : '否'
                        }
                    },
                    {
                        title: '操作',
                        align: 'center',
                        field: 'id',
                        width: 50,
                        formatter: function (val, row) {
                            var html = ' <a href="javascript:view_address(ID)">查看</a>'
                        <@app.has_oper oper_code='1_6_3'>
                            html += ' <a href="javascript:edit_address(ID)">修改</a>'
                        </@app.has_oper>
                        <@app.has_oper oper_code='1_6_4'>
                            html += ' <a href="javascript:remove_address(ID,defaultAddress)">删除</a>'
                        </@app.has_oper>
                            html = html.replace(/ID/g, row.id);
                            return html.replace('defaultAddress', row.defaultAddress);
                        }
                    }
                ]
            ],
            onLoadSuccess: function () {
                $('#page_table_address_${pid}').datagrid('clearChecked');
                $('#page_table_address_${pid}').datagrid('clearSelections');
            }
        });
        var ok = function () {
            return true;
        }

        $('#${pid}').data('ok', ok);
    })

    function reload_address() {
        $('#page_table_address_${pid}').datagrid('reload');
    }

    function add_address() {
        App.dialog.show({
            css: 'width:354px;height:305px;',
            title: '新建',
            href: "${contextPath}/security/basic/address_book/add.htm?passportId=${passportId}",
            event: {
                onClose: function () {
                    reload_address();
                }
            }
        });
    }
    function remove_address(id, defaultAddress) {
        if (defaultAddress == 1) {
            alert('当前地址为默认地址,不可删除!');
            return;
        }
        $.messager.confirm('提示信息', '确认删除?', function (ok) {
            if (ok) {
                $.post("${contextPath}/security/basic/address_book/delete.htm?id=" + id, function (json) {
                    if (json.success == 1) {
                        $.messager.alert('提示消息', '操作成功', 'info');
                        reload_address();
                    } else {
                        $.messager.alert('提示消息', json.message, 'info');
                    }
                }, 'json');
            }
        });
    }
    function view_address(id) {
        App.dialog.show({
            css: 'width:354px;height:305px;',
            title: '查看',
            href: "${contextPath}/security/basic/address_book/view.htm?id=" + id
        });
    }
    function edit_address(id) {
        App.dialog.show({
            css: 'width:354px;height:305px;',
            title: '修改',
            href: "${contextPath}/security/basic/address_book/edit.htm?id=" + id,
            event: {
                onClose: function () {
                    reload_address();
                }
            }
        });
    }
</script>
<div class="tab_item" style="display: block">
    <div class="toolbar clearfix">
        <div class="float_right">
        <@app.has_oper oper_code='1_6_2'>
            <button class="btn btn_red add" onclick="add_address()">新建</button>
        </@app.has_oper>
        </div>
        <h3>地址薄</h3>
    </div>
    <div class="grid" style="height:345px;">
        <table id="page_table_address_${pid}"></table>
    </div>
</div>
